package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.datatransferobject.UserLoginParam;
import cn.tedu._03vehicle.pojo.datatransferobject.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.valueobject.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("v1/user")
@Slf4j
@Tag(name = "01-用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperationSupport(order = 10)
    @PostMapping("/reg")
    @Operation(summary = "01-注册功能")
    public JsonResult usersRegParam(@Validated @RequestBody UserRegParam userRegParam) {
        log.debug("userRegParam:" + userRegParam);
        String username = userRegParam.getUsername();
        int count = userMapper.selectByUsername(username);
        if (count > 0) {
            return new JsonResult(StatusCode.OPERATION_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user:" + user);
        int num = userMapper.insertUser(user);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @ApiOperationSupport(order = 20)
    @PostMapping("/login")
    @Operation(summary = "02-登录功能")
    public JsonResult usersLoginParam(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam:" + userLoginParam);
        UserVO userVO = userMapper.selectByUserAndPass(userLoginParam);
        if (userVO == null) {
            return new JsonResult(StatusCode.OPERATION_PASSWORD_ERROR);
        }
        //将用户信息保存到session中
        session.setAttribute("user", userVO);
        //登陆成功
        return JsonResult.ok(userVO);
    }

    @ApiOperationSupport(order = 30)
    @GetMapping("/current")
    @Operation(summary = "03-获取当前用户")
    public JsonResult getCurrentUser(HttpSession session) {
        //服务器内存中获取会话标识
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @ApiOperationSupport(order = 40)
    @GetMapping("/logout")
    @Operation(summary = "04-退出登录")
    public void logOutUser(HttpSession session) {
        session.removeAttribute("user");
    }
}
